package org.hxzon.workeasy.pages.file;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;
import org.hxzon.workeasy.model.EasyFile;
import org.hxzon.workeasy.pages.base.MyBasePage;
import org.hxzon.workeasy.permissions.FileCRPermissions;
import org.hxzon.workeasy.service.EasyTagService;
import org.slf4j.Logger;

public class UploadFile extends MyBasePage {
    @Inject
    private Logger logger;
    @Inject
    private EasyTagService tagSer;
    @Property
    private List<UploadedFile> uploads;
    @Property
    private String tagString;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init() {
        new FileCRPermissions().check(null, getVisitor());
        uploads = new ArrayList<UploadedFile>();
        return null;
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "upload")
    Object uploadFile() {
        new FileCRPermissions().check(null, getVisitor());
        List<EasyFile> files = getWebUtil().upload(uploads, EasyFile.class);
        tagSer.saveFileAndTag(files, tagString);
        return null;
    }
}
